<!-- 新增编辑字典 -->
<template>
  <div>
    <el-dialog
      :title="isAdd == 1 ? '新增字典' : '修改字典'"
      :visible.sync="addEditVisible"
      :close-on-click-modal="false"
    >
      <el-form
        class="formView"
        ref="form"
        :model="formdata"
        :rules="rules"
        label-position="right"
        label-width="100px"
      >
        <el-form-item label="字典Key:" prop="dictKey">
          <el-input
            v-model.trim="formdata.dictKey"
            clearable
            placeholder="字典Key"
          ></el-input>
        </el-form-item>

        <el-form-item label="字典Value:" prop="value">
          <el-input
            type="textarea"
            v-model.trim="formdata.value"
            placeholder="字典Value"
          ></el-input>
        </el-form-item>

        <el-form-item label="字典Type:" prop="type">
          <el-input
            v-model.trim="formdata.type"
            placeholder="字典Type"
          ></el-input>
        </el-form-item>

        <el-form-item label="备注:" prop="remark">
          <el-input v-model="formdata.remark" placeholder="备注"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="addEditVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('form')">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { upDateDict, addDict } from '@/api/rightsManage'
export default {
  components: {},
  props: {
    isAdd: {
      type: Number,
      default() {
        return 1
      }
    },
    detailData: {
      type: Object,
      default() {
        return {}
      }
    },
    deptList: {
      type: Array,
      default() {
        return []
      }
    }
  },
  data() {
    return {
      addEditVisible: false,
      formdata: {
        dictKey: '',
        value: '',
        type: '',
        state: 1,
        remark: ''
      },
      channelPlatformList: [],
      channelTypeList: [],
      rules: {
        dictKey: [
          { required: true, message: '请输入字典key', trigger: 'blur' }
        ],
        value: [
          { required: true, message: '请输入字典value', trigger: 'blur' }
        ],
        type: [{ required: true, message: '请输入字典type', trigger: 'blur' }],
        remark: [{ required: true, message: '请输入字典备注', trigger: 'blur' }]
      }
    }
  },
  created() {},
  mounted() {},
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.onSubmit()
        } else {
          return false
        }
      })
    },
    onSubmit() {
      console.log(this.formdata)
      this.formdata.type = this.formdata.type.replace(/\s+/g, '')
      this.formdata.dictKey = this.formdata.dictKey.replace(/\s+/g, '')
      if (this.isAdd == 1) {
        addDict(this.formdata).then(res => {
          this.$message({
            message: this.isAdd == 1 ? '新增成功' : '修改成功',
            type: 'success'
          })
          this.$parent.getDataList()
          this.resetForm('form')
        })
      } else if (this.isAdd == 2) {
        this.formdata.dictId = this.formdata.id
        upDateDict(this.formdata).then(res => {
          this.$message({
            message: this.isAdd == 1 ? '新增成功' : '修改成功',
            type: 'success'
          })
          this.$parent.getDataList()
          this.resetForm('form')
        })
      }
    },
    resetForm(formName) {
      this.formdata = {
        dictKey: '',
        value: '',
        type: '',
        state: 1,
        remark: ''
      }
      this.addEditVisible = false
    }
  },
  watch: {
    addEditVisible: {
      deep: true,
      handler(now, old) {
        if (now) {
          if (this.isAdd == 2) {
            this.formdata = Object.assign({}, this.formdata, this.detailData)
          }
        } else {
          this.resetForm('form')
        }
      }
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep {
  .el-dialog {
    width: 500px;
    border-radius: 10px;
  }
  .el-dialog__title {
    font-size: 16px;
  }
  .el-input,
  .el-textarea {
    width: 250px;
  }
  .el-dialog__body {
    padding-top: 0;
    padding-bottom: 0px;
  }
  .el-form {
    padding: 10px 0;
  }
  .el-input__inner {
    height: 35px;
    line-height: 35px;
  }
  .el-form-item {
    margin-bottom: 15px;
    &:last-child {
      margin-bottom: 0;
    }
  }
  .el-dialog__footer {
    padding-top: 10px;
    padding-bottom: 10px;
    border-top: 1px solid #eee;
  }
}
.formView {
  border-top: 1px solid #eee;
  .sortInput {
    width: 100px;
  }
}
</style>
